var DURATION = 1000;

const keepOneDecimal = (value, type) => type==='int'?parseInt(value):Math.round(value * 10) / 10;

/**
 * 计算每帧数字
 * @param { number } startNum - 开始数字
 * @param { number } endNum - 结束数字
 * @param { number } start - 开始时间
 * @param { Function } callback - 回调函数
 */
const loop = (
  startNum,
  endNum,
  start,
  type,
  duration,
  callback
) => {
  requestAnimationFrame(() => {
    const now = Date.now();
    if(duration) DURATION = duration
    if (now > start + DURATION) {
      callback(endNum);
      return;
    }
    const percent = (now - start) / DURATION;
    const currentNum = keepOneDecimal((endNum - startNum) * percent, type) + startNum;
    callback(currentNum);

    loop(startNum, endNum, start, type, duration, callback);
  });
};

/**
 * 给数字添加动画效果
 * @param { number } startNum - 开始数字
 * @param { number } endNum - 结束数字
 * @param { Function } callback - 回调函数
 */
export const play = (
  startNum = 0,
  endNum,
  type='float',
  duration,
  callback
) => {
  const start = Date.now();
  loop(keepOneDecimal(startNum), endNum, start, type, duration, callback);
};
